#include "tasks.h"
#include "eeprom.h"
#include "devices.h"
#include "mems.h"

static uint8_t wake_esp_flg = 1;
static uint8_t mems_wake_flg = 1;

static void btn_task()
{
    if (btn_available() == 0)
        return;
    sys_restart_esp();
    sys_restart();
}

static void wake_esp_task()
{
    if (wake_esp_flg == 0)
        return;
    wake_esp_flg = 0;
    if (sys_chek_low_soc() == 1)
    {
        sys_power_off();
        return;
    }
    sys_wake_esp_up();
}

static void mems_chek_task()
{
    if (mems_wake_flg == 1)
    {
        mems_wake_flg = 0;
        if (sys_chek_low_soc() == 1)
        {
            mems_sleep();
            sys_power_off();
            return;
        }
        mems_chek();
    }
}

void tasks_init()
{
    esp_trans_task_init(); // 初始化主机通信
}

void tasks_wake_esp()
{
    wake_esp_flg = 1;
}

void tasks_memes_chek()
{
    mems_wake_flg = 1;
}

void task_loop_handler()
{
    btask_handler();
    btn_task();
    wake_esp_task();
    mems_chek_task();

    // btask_delay(500);
    // rtc_alarm_task();

    // sys_power_off();
    // btask_delay(1000);
}
